package com.lyf.emall.controller;

import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysRole;
import com.lyf.emall.service.RoleService;
import com.lyf.emall.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/8-11:10
 */
@RestController
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("/role")
    @PreAuthorize("hasAuthority('/role/list')")
    public ResponseResult getAllRole(Page page){
        return roleService.getAllRoleInfo(page);
    }

    @GetMapping("/role/admin")
    @PreAuthorize("hasAuthority('/role/admin')")
    public ResponseResult getAllRoleByAdmin(){
        return roleService.getAllRoleByAdmin();
    }

    @PostMapping("/role")
    @PreAuthorize("hasAuthority('/role/add')")
    public ResponseResult addRole(@RequestParam("roleName") String roleName,
                                  @RequestParam("authorityId") String authorityId,
                                  @RequestParam("description") String description,
                                  @RequestHeader String token) throws Exception {

        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();

        String[] split = authorityId.split(",");
        Integer[] id = new Integer[split.length];
        if (split.length != 0){
            for (int i = 0; i < split.length; i++){
                id[i] = Integer.valueOf(split[i]);
            }
        }

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysRole role = new SysRole(null, roleName, 1, 1, timestamp, timestamp, Integer.valueOf(userId), description);

        return roleService.addRole(role, id);
    }

    @GetMapping("/role/repeat")
    @PreAuthorize("hasAuthority('/role/repeat')")
    public ResponseResult isRole(@RequestParam("roleName") String roleName){
        return roleService.isRoleByRoleName(roleName);
    }

    @PutMapping("/role")
    @PreAuthorize("hasAuthority('/role/put')")
    public ResponseResult updateRoleInfo(@RequestParam("roleId") String roleId,
                                         @RequestParam("authorityId") String authorityId,
                                         @RequestParam("description") String description,
                                         @RequestHeader String token) throws Exception {
        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();

        String[] split = authorityId.split(",");
        Integer[] id = new Integer[split.length];
        if (split.length != 0){
            for (int i = 0; i < split.length; i++){
                id[i] = Integer.valueOf(split[i]);
            }
        }

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SysRole role = new SysRole(Integer.valueOf(roleId), null, null, null, null, timestamp, Integer.valueOf(userId), description);

        return roleService.updateRoleInfo(role, id);
    }

    @DeleteMapping("/role")
    @PreAuthorize("hasAuthority('/role/status')")
    public ResponseResult updateRoleStatus(@RequestParam("roleId") String roleId,
                                           @RequestParam("status") String status){
        return roleService.updateRoleStatus(Integer.valueOf(roleId), Integer.valueOf(status));
    }
}
